/**
 * 
 */
package client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UnicastRemoteObject;

import security.*;

import fanzone.FanZoneApp;

/**
 * @author zack
 *
 */
public class Client extends UnicastRemoteObject implements IClient {

	private static final long serialVersionUID = 1L;

	public Client() throws RemoteException {
		super();
	}

	/* (non-Javadoc)
	 * @see client.IClient#fetchResource(java.lang.String)
	 */
	@Override
	public byte[] fetchResource(String resourceName) throws RemoteException {

		System.out.println("Searching for resource " + resourceName + "...");

		File file = new File(FanZoneApp.RESOURCES_DIR + resourceName);

		InputStream is;
		byte[] bytes = null;
		try {
			is = new FileInputStream(file);

			// get the size of the file
			int length = (int) file.length();

			// create the byte array to hold the data with the length of File
			bytes = new byte[length];

			// read in the bytes
			is.read(bytes, 0, length);

			// close the input stream and return bytes
			is.close();
		} catch (FileNotFoundException e) {
			System.err.println("File not found while opening input stream");
		} catch (IOException e) {
			System.err.println("IO exception while reading from input stream");
			bytes = null; // discard whatever may have been read
		}

		if(bytes != null) // success
		{
			String remoteAddress = null;
			try {
				remoteAddress = UnicastRemoteObject.getClientHost();
				System.out.println("Sent resource " + resourceName +
						" to host @ " + remoteAddress);
			} catch (ServerNotActiveException e) {
				// do nothing
			}
		}
		return bytes;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {

			// path for the policy.all file
			System.getProperties().put( "java.security.policy",
					"src/servers/policy.all");

			KeyManager.getInstance();
			KeyManager.makePublicCert("clientserver");
			
			if( System.getSecurityManager() == null) {
				System.setSecurityManager( new RMISecurityManager());
			}

			try { // start rmiregistry
				LocateRegistry.createRegistry( 1099);
			} catch( RemoteException e) { // if not start it
				// do nothing : already started with 'rmiregistry'
			}

			IClient clientServer = new Client();
			
			RMIClientSocketFactory csf = new CipherClientSocketFactory();
	        RMIServerSocketFactory ssf = new CipherServerSocketFactory();
	        IClient stub = (IClient) UnicastRemoteObject.exportObject(clientServer, 0, csf, ssf);
			
			Naming.rebind( "/clientServer", stub);
			System.out.println( "clientServer bound in registry");
		} catch( Throwable th) {
			th.printStackTrace();
		}
	}

}
